﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pavilion.Communications;

namespace Pavilion.DesignerTool
{
    static public class NodeTransfer
    {
        static public void Add(string resourceName, string scriptName, NodeBase node)
        {
            var containerNodeNameList = CreateNodeStringCollection(node.ParentContainer);

            DataTransferClient.Instance.TransferData((proxy) => proxy.AddNode(resourceName, scriptName, containerNodeNameList, node.Name, node.GetType().Name));

            AddInternalNodeContent(resourceName, scriptName, node);
        }

        static public void AddInternalNodeContent(string resourceName, string scriptName, NodeBase node)
        {
            foreach (GameData gameData in node.GameDataCollection)
                GameDataTransfer.AddGameData(resourceName, scriptName, node, gameData);

            if (node is ContainerNode)
            {
                ContainerNode containerNode = node as ContainerNode;

                foreach (NodeBase newNode in containerNode.NodeCollection)
                {
                    if (!(newNode is EntryNode))
                        Add(resourceName, scriptName, newNode);
                }

                foreach (Link newLink in containerNode.LinkCollection)
                    LinkTransfer.Add(resourceName, scriptName, containerNode, newLink);
            }
            else if (node is AnimationNode)
            {
                UpdateAnimationNode(resourceName, scriptName, node as AnimationNode);
            }
        }

        static public void Rename(string resourceName, string scriptName, NodeBase node, string oldNodeName, string newNodeName)
        {
            var containerNodeNameList = CreateNodeStringCollection(node.ParentContainer);

            DataTransferClient.Instance.TransferData((proxy) => proxy.RenameNode(resourceName, scriptName, containerNodeNameList, oldNodeName, newNodeName));
        }

        static public void Remove(string resourceName, string scriptName, ContainerNode parentContainer, NodeBase node)
        {
            var containerNodeNameList = CreateNodeStringCollection(parentContainer);

            DataTransferClient.Instance.TransferData((proxy) => proxy.RemoveNode(resourceName, scriptName, containerNodeNameList, node.Name));
        }

        static public void UpdateAnimationNode(string resourceName, string scriptName, AnimationNode animationNode)
        {
            var containerNodeNameList = CreateNodeStringCollection(animationNode.ParentContainer);

            DataTransferClient.Instance.TransferData((proxy) => proxy.UpdateAnimationNode(resourceName, scriptName, containerNodeNameList, animationNode.Name, animationNode.Animation.Name));
        }

        static public List<string> CreateNodeStringCollection(ContainerNodeWrapper containerNodeWrapper)
        {
            return CreateNodeStringCollection(containerNodeWrapper.ContainerNode);
        }

        static public List<string> CreateNodeStringCollection(ContainerNode containerNode)
        {
            List<string> containerNodeNameList = new List<string>();

            while (containerNode != null)
            {
                containerNodeNameList.Insert(0, containerNode.Name);
                containerNode = containerNode.ParentContainer;
            }

            return containerNodeNameList;
        }
    }
}
